<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB, Session;

class CommentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //查询用户评价信息
        $comments = DB::table("comment")->leftJoin('comment_reply', 'comment.id', '=', 'comment_reply.comment_id')
                ->select(['comment.*', 'comment_reply.reply_time'])
                ->orderBy('comment.id','desc')
                ->paginate(15);
        foreach($comments as $comment){
            $comment->goods_name = DB::table('goods')->where('id', $comment->goods_id)->pluck('goods_name');
            $comment->user_name = DB::table('user')->where('id', $comment->user_id)->pluck('user_nickname');
        }
        //加载模板显示
        return view("admin.comment.index", compact("comments"));                
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // 获取评价信息
        $comment = DB::table('comment')->find($id);
        // 获取回复评价信息
        $reply = DB::table('comment_reply')->where('comment_id', $comment->id)->first();
        
        $comment->goods_name = DB::table('goods')->where('id', $comment->goods_id)->pluck('goods_name');
        $comment->attr_type = DB::table('goods_attr')->where('id', $comment->attr_id)->pluck('attr_type');
        $comment->user_name = DB::table('user')->where('id', $comment->user_id)->pluck('user_nickname');
        $compact = ['comment'=>$comment];
        if(false != $reply){
            $compact['reply'] = $reply;
        }
        return view('admin.comment.edit', $compact);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // 获取status
        $status = $request->get('comment_status');
        $content = $request->get('re_content');
        if(trim($content)){
            // 先查询是否存在
            $table = DB::table('comment_reply');
            $res = $table->where('comment_id', $id)->first();
            if($res){
                $table->where('comment_id', $id)->update(['re_content'=>$content]);
            } else {
                $table->insert(['comment_id'=>$id,'reply_content'=>$content,'reply_time'=>date('Y-m-d H:i:s')]);
            }
        }
        
        DB::table('comment')->where('id',$id)->update(['comment_status'=>$status]);
        return redirect('/admin/comment');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DB::table('comment')->where('id', $id)->update(['comment_status'=>0]);
        return redirect('/admin/comment');
    }
}
